<?php
/**
 * Admin functions.
 *
 * @package News
 * @subpackage Functions
 */

/* Load theme settings meta boxes. */
add_action( 'load-appearance_page_theme-settings', 'news_create_settings_meta_boxes' );

/* Additional theme settings. */
add_filter( hybrid_get_prefix() . '_settings_args', 'news_theme_settings' );

/* Validate theme settings. */
add_filter( hybrid_get_prefix() . '_validate_theme_settings', 'news_validate_theme_settings' );

/* Flush rewrite rules for custom post types. */
add_action( 'load-themes.php', 'news_flush_rewrite_rules' );

/**
 * Flushes the rewrite rules on theme activation so that custom post type permalinks work correctly.
 *
 * @since 0.1.0
 */
function news_flush_rewrite_rules() {
	global $pagenow, $wp_rewrite;

	if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
		$wp_rewrite->flush_rules();
}

/**
 * Add additional settings to the theme settings array.
 *
 * @since 0.1.0
 */
function news_theme_settings( $settings ) {
	$settings['home_template_categories'] = array();
	return $settings;
}

/**
 * Validates the theme settings.
 *
 * @since 0.1.0
 */
function news_validate_theme_settings( $settings ) {
	$settings['home_template_categories'] = ( ( isset( $settings['home_template_categories'] ) && is_array( $settings['home_template_categories'] ) ) ? array_map( 'absint', $settings['home_template_categories'] ) : array() );

	return $settings;
}

/**
 * Add meta boxes to the theme settings page.
 *
 * @since 0.1.0
 */
function news_create_settings_meta_boxes() {
	add_meta_box( "news-home-template-meta-box", __( 'Home Page Template Settings', hybrid_get_textdomain() ), 'news_home_template_theme_meta_box', 'appearance_page_theme-settings', 'normal', 'low' );
 }

/**
 * Display the home template meta box.
 *
 * @since 0.1.0
 */
function news_home_template_theme_meta_box() {
	$prefix = hybrid_get_prefix();

	/* Get theme settings from the database. */
	$settings = get_option( "{$prefix}_theme_settings" );

	/* Settings used. */
	$category_highlight = ( !empty( $settings['home_template_categories'] ) ? $settings['home_template_categories'] : array() );

	/* Get categories. */
	$categories = get_categories(); ?>

	<table class="form-table">

		<tr>
			<th><?php _e( 'About:', hybrid_get_textdomain() ); ?></th>
			<td>
				<?php _e( 'Settings used on pages that use the "Home" page template.  This template must be assigned to a page before its settings take effect.', hybrid_get_textdomain() ); ?>
			</td>
		</tr>
		<tr>
			<th><label for="home_template_categories"><?php _e( 'Category Highlight:', hybrid_get_textdomain() ); ?></label></th>
			<td>
				<label for="home_template_categories"><?php _e( 'Categories to show blog posts from in the category highlight section.  Multiple categories may be chosen by holding the <code>Ctrl</code> key and selecting.', hybrid_get_textdomain() ); ?></label>
				<br />
				<select id="home_template_categories" name="home_template_categories[]" multiple="multiple" style="height:150px;">
				<?php foreach( $categories as $cat ) { ?>
					<option value="<?php echo $cat->term_id; ?>" <?php if( is_array( $category_highlight ) && in_array( $cat->term_id, $category_highlight ) ) echo ' selected="selected"'; ?>><?php echo esc_html( $cat->name ); ?></option>
				<?php } ?>
				</select>
			</td>
		</tr>

	</table><?php
}

?>